<?php
/**
 * common repository file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/08/20
 * Time: 06:07
 */

namespace HiCommon\Repository;

use HiCommon\Model\Goods;

class GoodsRepository extends BaseRepository
{
    public function base_model()
    {
        return new \HiCommon\Model\Goods();
    }

    public function get_all_to_shop($arr_option = [])
    {
        return Goods::where(function ($query) {
            $query->orWhere('type', 1)->orWhere('type', 3);
        })->commonget($arr_option);
    }

    /**
     * 分页获取全部数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all($search = [])
    {
        $model = new Goods();
        if (isset($search['name']) && $search['name']) {
            $model = $model->where('name', 'like', '%' . $search['name'] . '%');
        }
        if (isset($search['cate_id']) && $search['cate_id']) {
            $model = $model->where('cate_id', $search['cate_id']);
        }
        if (isset($search['type']) && $search['type']) {
            $model = $model->where('type', $search['type']);
        }
        if (isset($search['status']) && $search['status']) {
            $model = $model->where('status', $search['status']);
        }
        if (isset($search['shop']) && $search['shop']) {
            $model = $model->where('repairstore_id', $search['shop']);//维修店
        } else {
            $model = $model->where('repairstore_id', 1);//主平台
        }

        return $model->with(['cate'])->commonpage();
    }

    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list($arr_option = [])
    {
        return Goods::commonpage($arr_option);
    }

    /**模糊搜索
     * @param array $arr_option
     * @return mixed
     */
    public function search_goods_list($arr_option = [])
    {
        $model = new Goods();
        if (isset($arr_option['like']) && $arr_option['like']) {
            $model = $model->where('name', 'like', '%' . $arr_option['like'] . '%');
        }

        //价格区间
        if (isset($arr_option['map']['min_price']) || isset($arr_option['map']['max_price'])) {
            $between = [$arr_option['map']['min_price'], $arr_option['map']['max_price']];
            $model = $model->where(function ($query) use ($between) {
                if ($between[0] > 0 && $between[1] > 0) {
                    $query->whereBetween('min_price', $between);
                }else{
                    if ($between[0] > 0) {
                        $query->where('min_price', '>=', $between[0]);
                    }
                    if ($between[1] > 0) {
                        $query->where('min_price', '<=', $between[1]);
                    }
                }

                //$query->whereBetween('min_price', $between)->orWhereBetween('max_price', $between);
            });
        }

        //服务标签
        if (isset($arr_option['map']['sale_tag'])) {
            $sale_tag = $arr_option['map']['sale_tag'];
            $model = $model->whereHas('sale_tag_relation', function ($query) use ($sale_tag) {
                $query->where('sale_tag_id', $sale_tag);
            });
        }

        //是否有库存
        if (isset($arr_option['map']['stock'])) {
            $model = $model->whereHas('product_info', function ($query) {
                $query->where('stock', '>=', 1);
            });
        }

        return $model->commonpage($arr_option);
    }

    /**
     * 通过where分页获取数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list_by_where($arr_where, $arr_option = [])
    {
        return Goods::where($arr_where)->commonpage($arr_option);
    }


    /**
     *  根据指定条件获取数据
     *  hinq 2019 09 03
     *
     **/
    public function get_my_coupon_to_goods(array $arr_option)
    {
        return Goods::commonget($arr_option);
    }

    /**
     *  根据指定条件获取数据[商户app添加活动获取商品 ]
     *  hinq 2019 10 30
     *
     **/
    public function get_goods_list_ids(array $arr_option, array $arr_where)
    {
        return Goods::whereNotIn($arr_where['field'], $arr_where['ids'])->commonpage($arr_option);
    }


    /**
     *  根据指定条件获取数据[后台使用的 ]
     *  hinq 2019 09 03
     *
     **/
    public function get_wherein_activity_to_goods(array $arr_option, array $arrWhereIn)
    {
        return Goods::whereIn($arrWhereIn['field'], $arrWhereIn['ids'])->commonget($arr_option);
    }


    public function get_wherenotin_activity_to_goods(array $arr_option, array $arr_where)
    {
        return Goods::whereNotIn($arr_where['field'], $arr_where['ids'])->commonget($arr_option);
    }


    public function get_where_activity_to_goods(array $arr_option)
    {
        return Goods::commonget($arr_option);
    }


    /**
     *  根据指定条件获取指定数量的数据
     *  hinq 2019 09 03
     *
     **/
    public function get_list_where_limit(array $arr_where, $num = 3, array $field = ['*'])
    {
        return Goods::where($arr_where)->with(['product_info'])->orderBy('id', 'desc')->limit($num)->get($field);
    }


    /**
     * 用户API接口用
     */
    public function by_where_limit(array $option)
    {
        return Goods::where($option['where'])
            ->with($option['with'])
            ->orderBy('updated_at', 'desc')
            ->limit($option['limit'])
            ->get($option['field'])
            ->toArray();
    }


    /**
     * 根据条件统计数量
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_by_where($arr_where)
    {
        return Goods::where($arr_where)->count();
    }

    /**
     * 根据条件统计总和
     *
     * @param array $arr_where
     * @param string $s_field
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_sum_by_where($arr_where = [], $s_field = 'mark')
    {
        return Goods::where($arr_where)->sum($s_field);
    }

    /**
     * 添加一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one($arr_input)
    {
        return Goods::create($arr_input);
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id, $arr_option = [])
    {
        return Goods::commonfind($n_id, $arr_option);
    }


    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        return Goods::where('id', $arr_input['id'])->update($arr_input);
    }

    /**
     * 更改多条数据
     */
    public function edit_whereIn(array $ids, array $data)
    {
        return Goods::whereIn('id', $ids)->update($data);
    }


    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one(int $n_id)
    {
        return Goods::where('id', $n_id)->commondelete();
    }

    /**
     * 删除多条数据
     */
    public function delete_more(array $id_array)
    {
        return Goods::whereIn('id', $id_array)->commondelete();
    }

    /**
     * 根据条件修改数据
     *
     * @param int $n_id
     * @param int $n_state
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_status(array $map, array $updata)
    {
        return Goods::whereIn('id', $map)->update($updata);
    }

    /**
     * 字段自增
     *
     * @param int $n_id
     * @param string $s_field
     * @param int $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function increase_num($n_id, $n_step = 1, $s_field = 'sell')
    {
        return Goods::where('id', $n_id)->increment($s_field, $n_step);
    }

    /**
     * 字段自减
     *
     * @param int $n_id
     * @param string $s_field
     * @param int $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function decrease_num($n_id, $s_field = 'commend_num', $n_step = 1)
    {
        return Goods::where('id', $n_id)->decrement($s_field, $n_step);
    }

    /**
     * 该门店已有的商品分类
     */
    public function get_cate_list($array_where = [])
    {
        return Goods::where($array_where)->groupBy('cate_id')->with(['cate'])->get(['id', 'cate_id', 'type'])->toArray();
    }

}